using System;
using System.Collections;
using System.Collections.Generic;
using BitMango;
using BitMango.Diagnostics;
using Holoville.HOTween;
using UnityEngine;

public sealed class SoundManager : SingletonMonoBehaviour<SoundManager>
{
    protected override void Awake()
    {
        base.Awake();
        this.sounds = new GameObject("+SoundManager");
        if (this.clips == null)
        {
            Debugger.LogWarning("SoundManager.Awake : no clips");
            return;
        }
        foreach (AudioClip audioClip in this.clips)
        {
            if (audioClip != null)
            {
                this.audioClips.Add(audioClip.name, audioClip);
            }
        }
        for (int j = 0; j < this.BGMChannelCount; j++)
        {
            this.CreateAudioSource();
            this.bgmChannels.Add(j);
        }
    }

    public void Play(string soundName, float volume = 1f, float pitch = 1f, bool loop = false)
    {
        AudioClip audioClip = this.audioClips[soundName] as AudioClip;
        if (audioClip == null)
        {
            Debugger.LogWarning(string.Format("Can't found audio clip - {0}", soundName));
            return;
        }
        AudioSource soundPlayer = this.GetSoundPlayer();
        soundPlayer.clip = audioClip;
        soundPlayer.loop = loop;
        soundPlayer.volume = volume;
        soundPlayer.pitch = pitch;
        soundPlayer.Play();
    }

    public void PlayBGM(string soundName, float volume = 1f, int channel = 0, bool loop = true, bool useFade = true)
    {
        Debugger.Log(string.Concat(new object[]
        {
            "SoundManager.PlayBGM : ",
            soundName,
            " ch:",
            channel
        }));
        AudioClip clip = this.audioClips[soundName] as AudioClip;
        if (clip == null)
        {
            Debugger.LogWarning(string.Format("Can't found audio clip - {0}", soundName));
            return;
        }
        HOTween.Complete("BGM_Fade" + channel);
        AudioSource bgm = this.soundPlayers[channel];
        Debugger.Assert(bgm != null, "SoundManager : PlayBGM - bgm is null");
        if (!this.bgmChannels.Contains(channel))
        {
            this.bgmChannels.Add(channel);
        }
        if (bgm.isPlaying)
        {
            if (bgm.clip == clip)
            {
                return;
            }
            if (useFade)
            {
                Sequence sequence = new Sequence(new SequenceParms().Id("BGM_Fade" + channel));
                sequence.Append(HOTween.To(bgm, 0.5f, "volume", 0));
                sequence.AppendInterval(0.3f);
                sequence.AppendCallback(delegate (TweenEvent parm)
                {
                    bgm.clip = clip;
                    bgm.loop = loop;
                    bgm.Play();
                }, new object[0]);
                sequence.Append(HOTween.To(bgm, 0.5f, "volume", volume));
                sequence.Play();
            }
            else
            {
                bgm.clip = clip;
                bgm.loop = loop;
                bgm.Play();
            }
        }
        else
        {
            bgm.clip = clip;
            bgm.loop = loop;
            if (useFade)
            {
                bgm.volume = 0f;
                Sequence sequence2 = new Sequence(new SequenceParms().Id("BGM_Fade" + channel));
                sequence2.Append(HOTween.To(bgm, 0.5f, "volume", volume));
                sequence2.Play();
            }
            else
            {
                bgm.volume = volume;
            }
            bgm.Play();
        }
    }

    public void StopBGM()
    {
        foreach (int num in this.bgmChannels)
        {
            AudioSource bgm = this.soundPlayers[num];
            Debugger.Assert(bgm != null, "SoundManager : StopBGM - bgm is null");
            if (bgm.isPlaying)
            {
                HOTween.Complete("BGM_Fade" + num);
                Sequence sequence = new Sequence(new SequenceParms().Id("BGM_Fade" + num));
                sequence.Append(HOTween.To(bgm, 0.5f, "volume", 0));
                sequence.AppendCallback(delegate (TweenEvent parm)
                {
                    bgm.Stop();
                }, new object[0]);
                sequence.Play();
            }
        }
    }

    public void SetBGMVolume(float volume)
    {
        Debugger.Assert(this.bgmChannels != null, "SoundManager : SetBGMVolume - bgmChannel is null");
        foreach (int index in this.bgmChannels)
        {
            this.soundPlayers[index].volume = volume;
        }
    }

    public bool IsPlaySound(string soundName)
    {
        AudioClip y = this.audioClips[soundName] as AudioClip;
        foreach (AudioSource audioSource in this.soundPlayers)
        {
            if (!(audioSource == null))
            {
                if (audioSource.isPlaying && audioSource.clip == y)
                {
                    return true;
                }
            }
        }
        return false;
    }

    public void Stop(string soundName)
    {
        AudioClip y = this.audioClips[soundName] as AudioClip;
        foreach (AudioSource audioSource in this.soundPlayers)
        {
            if (!(audioSource == null))
            {
                if (audioSource.isPlaying && audioSource.clip == y)
                {
                    audioSource.Stop();
                }
            }
        }
    }

    public bool IsExistSound(string soundName)
    {
        AudioClip x = this.audioClips[soundName] as AudioClip;
        return !(x == null);
    }

    private AudioSource CreateAudioSource()
    {
        GameObject gameObject = new GameObject("SoundPlayer");
        Debugger.Assert(gameObject != null, "SoundManager : CreateAudioSource - SoundPlayer Gameobject is null");
        AudioSource audioSource = gameObject.AddComponent<AudioSource>();
        Debugger.Assert(this.sounds.transform != null, "SoundManager : CreateAudioSource - SoundPlayer Gameobject parent is null");
        gameObject.transform.parent = this.sounds.transform;
        this.soundPlayers.Add(audioSource);
        return audioSource;
    }

    private AudioSource GetSoundPlayer()
    {
        for (int i = 2; i < this.soundPlayers.Count; i++)
        {
            AudioSource audioSource = this.soundPlayers[i];
            if (!audioSource.GetComponent<AudioSource>().isPlaying)
            {
                return audioSource;
            }
        }
        return this.CreateAudioSource();
    }

    public AudioClip[] clips;

    public int BGMChannelCount = 1;

    private Hashtable audioClips = new Hashtable();

    private List<AudioSource> soundPlayers = new List<AudioSource>();

    private GameObject sounds;

    private List<int> bgmChannels = new List<int>();
}
